package com.loongtech.bi.action.system;

import com.loongtech.bi.entity.system.EntityConfig;
import com.loongtech.bi.manager.system.ConfigManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 配置管理
 * 
 * @author xckuang
 * @date 2014-10-02
 */
@Controller("configAction")
@Scope("view")
public class ConfigAction implements Serializable {
	private static final long serialVersionUID = 1L;
	@Autowired
	private Session session;
	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private ConfigManager configManager;

	private List<EntityConfig> configList = new ArrayList<EntityConfig>();
	private EntityConfig config = new EntityConfig();

	@Autowired
	public void init() {
		findAll();
	}

	public void add() {
		configManager.persist(config);
		sysLogManager.addLog(session.getUserName(), "增加配置数据"+config.getEnumName());
		FacesContext context = FacesContext.getCurrentInstance();
		context.addMessage(null, new FacesMessage("增加配置数据成功", "增加配置数据成功"+config.getEnumName()));
		init();
        configManager.init();
	}

	public void modify(EntityConfig entity) {
		FacesContext context = FacesContext.getCurrentInstance();
		EntityConfig cur = configManager.get(entity.getId());
		if (cur == null)
		{
			context.addMessage(null, new FacesMessage("修改配置数据失败", "修改配置数据失败，对应配置不存在"+entity.getEnumName()));
			return;
		}
		configManager.merge(entity);
		sysLogManager.addLog(session.getUserName(), "修改配置数据"+entity.getEnumName());
		context.addMessage(null, new FacesMessage("修改配置数据成功", "修改配置数据成功"+entity));
		findAll();
        configManager.init();
	}

	public void findAll() {
		configList.clear();
		configList.addAll(configManager.findAll());
	}

	public List<EntityConfig> getConfigList() {
		return configList;
	}

	public void setConfigList(List<EntityConfig> configList) {
		this.configList = configList;
	}

	public EntityConfig getConfig() {
		return config;
	}

	public void setConfig(EntityConfig config) {
		this.config = config;
	}
}
